home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_Event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  11.8 KB  |  534 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Event.c
  3.  
  4.     Contains:    Event dispatching and event handlers.
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     04/23/95    RB        Moving CADispatchMenuEvent() to Menu_HandleMenu()
  13.                                     Added Adjust menu calls to Event_DoMouseInMenuBar()
  14.          <2>     01/17/95    DAS        changed all FrontWindow() calls to
  15.                                      App_GetFrontDocWindow() to account for
  16.                                      floating windows.
  17.                   12/22/94    DHN        Created from existing Light software 
  18.                                      application.
  19.          ---------------
  20.          <1>     11/20/93    DHN        Created.
  21. */
  22.  
  23. #ifdef USE_CALIB
  24. #include "CALib.h"
  25. #endif
  26.  
  27. #include "CAS_Globals.h"
  28. #include "CAS_Misc.h"
  29. #include "CAS_App.h"
  30. #include "CAS_Win.h"
  31. #include "CAS_Event.h"
  32. #include "CAS_HelpBalloon.h"
  33. #include "CAS_AppleEvent.h"
  34. #include "CAS_Menu.h"
  35.  
  36.  
  37. //----------------------------------------------------------------------
  38. //     local prototypes
  39.  
  40. #if defined(__cplusplus)
  41. extern "C"
  42. {
  43. #endif
  44.  
  45. static void Event_DoMouseInSysWindow(
  46.     EventRecord        *theEvent,
  47.     WindowPtr        theWindow );
  48. static void Event_DoMouseInMenuBar(
  49.     EventRecord        *theEvent,
  50.     WindowPtr        theWindow );
  51. static void Event_DoMouseInDrag(
  52.     EventRecord        *theEvent,
  53.     WindowPtr        theWindow );
  54. static void Event_DoMouseInGoAway(
  55.     EventRecord        *theEvent,
  56.     WindowPtr        theWindow );
  57. static void Event_DoMouseInZoomIn(
  58.     EventRecord        *theEvent,
  59.     WindowPtr        theWindow );
  60. static void Event_DoMouseInZoomOut(
  61.     EventRecord        *theEvent,
  62.     WindowPtr        theWindow );
  63. static void Event_DoMouseInGrow(
  64.     EventRecord        *theEvent,
  65.     WindowPtr        theWindow );
  66. static void Event_DoMouseInContent(
  67.     EventRecord        *theEvent,
  68.     WindowPtr        theWindow );
  69. static void Event_DoKeyDownEvent(
  70.     EventRecord        *theEvent,
  71.     WindowPtr        theWindow );
  72. static void Event_DoCmdKeyDownEvent(
  73.     EventRecord        *theEvent,
  74.     WindowPtr        theWindow );
  75.  
  76. #if defined(__cplusplus)
  77. }
  78. #endif
  79.  
  80. //---------------------------------------------------------------------------
  81.  
  82. Boolean Event_DoubleClick(
  83.     EventRecord        *theEvent )
  84. {
  85.     return
  86.         ((TickCount() - gMouseUpTickCount < LMGetDoubleTime())
  87.         && bAlmostEqual( theEvent->where, gMouseUpPoint, 4 ));
  88. }
  89.  
  90. //---------------------------------------------------------------------------
  91. // Event_DoIdle - calls appIdle to let the application do any idle tasks, then handles
  92. // any standard idle tasks.
  93.  
  94. void Event_DoIdle( void )
  95. {
  96. WindowPtr    theWindow;
  97.  
  98.     theWindow = App_GetFrontDocWindow();
  99.  
  100.     App_Idle( theWindow );
  101.     doBalloonHelp( theWindow );
  102. }
  103.  
  104. //---------------------------------------------------------------------------
  105.  
  106. void Event_HandleNullEvent(
  107.     EventRecord        *theEvent )
  108. {
  109.     Event_DoIdle();
  110. }
  111.  
  112. //---------------------------------------------------------------------------
  113.  
  114. static void Event_DoMouseInSysWindow(
  115.     EventRecord        *theEvent,
  116.     WindowPtr        theWindow )
  117. {
  118.     SystemClick( theEvent, theWindow );
  119. }
  120.  
  121. //---------------------------------------------------------------------------
  122.  
  123. static void Event_DoMouseInMenuBar(
  124.     EventRecord        *theEvent,
  125.     WindowPtr        theWindow )
  126. {
  127. long    mSelector;
  128.  
  129. #ifdef USE_CALIB
  130.  
  131.     if (gCALibExists)
  132.     {
  133.         OSErr        theErr;
  134.  
  135.         CAAdjustMenus();
  136.         if (theErr = CAError())
  137.             ;        // handle the error
  138.     }
  139.  
  140. #endif
  141.  
  142.     Menu_AdjustMenus(theWindow);
  143.  
  144.     mSelector = MenuSelect( theEvent->where );
  145.  
  146.     Menu_HandleMenu( mSelector, theWindow );
  147. }
  148.  
  149. //---------------------------------------------------------------------------
  150.  
  151. static void Event_DoMouseInDrag(
  152.     EventRecord        *theEvent,
  153.     WindowPtr        theWindow )
  154. {
  155.     // if it's a doc window, select it and drag it.
  156.     if (Win_IsAppWindow( theWindow ) ||
  157.         (CAIsPartWindow (theWindow) == kCADocumentWindow))
  158.     {
  159.         // DragWindow looks for the special case where the dragRect is the size
  160.         // of screenBits.bounds so this should work on multi-monitor systems.
  161.         Rect dragRect = qd.screenBits.bounds;            // $$$$$ a fine choice?
  162.  
  163.         // select the window.
  164.         App_SetFrontDocWindow( theWindow ); // was SelectWindow()
  165.  
  166.         // Drag the window (was DragWindow()). $$$$$ This will not inherit new DragWindow behavior.
  167.         Win_DragWindow_Better( theWindow, theEvent->where, &dragRect );
  168.  
  169.         // see if the app wants to adjust anything.
  170.         App_Drag( theWindow );
  171.     }
  172. }
  173.  
  174. //---------------------------------------------------------------------------
  175.  
  176. static void Event_DoMouseInGoAway(
  177.     EventRecord        *theEvent,
  178.     WindowPtr        theWindow )
  179. {
  180.     if (TrackGoAway( theWindow, theEvent->where ))
  181.     {
  182.         // if the option key is down, close all the windows.
  183.         if (gOptionPressed)
  184.             App_CloseAll();
  185.         else
  186.             Win_Close( theWindow, NULL );
  187.     }
  188. }
  189.  
  190. //---------------------------------------------------------------------------
  191.  
  192. static void Event_DoMouseInZoomIn(
  193.     EventRecord        *theEvent,
  194.     WindowPtr        theWindow )
  195. {
  196.     if (TrackBox( theWindow, theEvent->where, inZoomIn ))
  197.     {
  198.         EraseRect( &theWindow->portRect );
  199.         ZoomWindow( theWindow, inZoomIn, false );
  200.         App_ZoomIn( theWindow );
  201.     }
  202. }
  203.  
  204. //---------------------------------------------------------------------------
  205.  
  206. static void Event_DoMouseInZoomOut(
  207.     EventRecord        *theEvent,
  208.     WindowPtr        theWindow )
  209. {
  210.     if (TrackBox( theWindow, theEvent->where, inZoomOut ))
  211.     {
  212.         EraseRect( &theWindow->portRect );
  213.         ZoomWindow( theWindow, inZoomOut, false );
  214.         App_ZoomOut( theWindow );
  215.     }
  216. }
  217.  
  218. //---------------------------------------------------------------------------
  219.  
  220. static void Event_DoMouseInGrow(
  221.     EventRecord        *theEvent,
  222.     WindowPtr        theWindow )
  223. {
  224. Rect    wBounds, growRect;
  225. Handle    theRectHandle;
  226. long    theResult;
  227.  
  228.     growRect = theWindow->portRect;
  229.     growRect.left = growRect.right - (SBARWIDTH-1);
  230.     growRect.top = growRect.bottom - (SBARWIDTH-1);
  231.  
  232.     // if the cmd key is down, just use our bounds above.
  233.     if (!gCmdPressed)
  234.     {
  235.         // otherwise get the bounds from the wrct resource.
  236.         theRectHandle = GetIndResource( 'wrct', 1 );
  237.         if (theRectHandle != nil)
  238.             wBounds = **(Rect**)theRectHandle;
  239.     }
  240.  
  241.     SetRect( &wBounds, 140, 110, 32767, 32767 );
  242.     theResult = GrowWindow( theWindow, theEvent->where, &wBounds );
  243.     if (theResult != 0L)
  244.     {
  245.         SizeWindow( theWindow, LoWord( theResult ), HiWord( theResult ), true );
  246.  
  247.         InvalRect( &growRect );
  248.  
  249.         App_Grow( theWindow );
  250.  
  251.         DrawGrowIcon( theWindow );
  252.     }
  253. }
  254.  
  255. //---------------------------------------------------------------------------
  256.  
  257. static void Event_DoMouseInContent(
  258.     EventRecord        *theEvent,
  259.     WindowPtr        theWindow )
  260. {
  261.     if (Win_IsAppWindow( theWindow ))
  262.     {
  263.         if (theWindow != App_GetFrontDocWindow())
  264.             App_SetFrontDocWindow( theWindow );
  265.         else
  266.         {
  267.             SetPort( (GrafPtr)theWindow );
  268.             App_MouseInContent( theEvent, theWindow );
  269.         }
  270.     }
  271. }
  272.  
  273. //---------------------------------------------------------------------------
  274.  
  275. void Event_HandleMouseDownEvent(
  276.     EventRecord        *theEvent )
  277. {
  278. WindowPtr    theWindow;
  279. short        windowCode;
  280.  
  281.     windowCode = FindWindow( theEvent->where, &theWindow );
  282.     if ((theWindow != nil) && (windowCode != inMenuBar))
  283.         SetPort( (GrafPtr)theWindow );
  284.  
  285.     switch (windowCode)
  286.     {
  287.         case inSysWindow:
  288.             Event_DoMouseInSysWindow( theEvent, theWindow );
  289.             break;
  290.  
  291.         case inMenuBar:
  292.             // click in menu bar applies to frontDocWindow, not theWindow
  293.             Event_DoMouseInMenuBar( theEvent, App_GetFrontDocWindow() );
  294.             //Event_DoMouseInMenuBar( theEvent, theWindow );
  295.             break;
  296.  
  297.         case inDrag:
  298.             Event_DoMouseInDrag( theEvent, theWindow );
  299.             break;
  300.  
  301.         case inContent:
  302.             Event_DoMouseInContent( theEvent, theWindow );
  303.             break;
  304.  
  305.         case inGoAway:
  306.             Event_DoMouseInGoAway( theEvent, theWindow );
  307.             break;
  308.  
  309.         case inZoomIn:
  310.             Event_DoMouseInZoomIn( theEvent, theWindow );
  311.             break;
  312.  
  313.         case inZoomOut:
  314.             Event_DoMouseInZoomOut( theEvent, theWindow );
  315.             break;
  316.  
  317.         case inGrow:
  318.             Event_DoMouseInGrow( theEvent, theWindow );
  319.             break;
  320.  
  321.         default:
  322.             break;
  323.     }
  324. }
  325.  
  326. //---------------------------------------------------------------------------
  327.  
  328. void Event_HandleMouseUpEvent(
  329.     EventRecord        *theEvent )
  330. {
  331.     // save the ticks and position of the mouse for double-click checking
  332.     gMouseUpTickCount = TickCount();
  333.     gMouseUpPoint = theEvent->where;
  334. }
  335.  
  336. //---------------------------------------------------------------------------
  337.  
  338. static void Event_DoKeyDownEvent(
  339.     EventRecord        *theEvent,
  340.     WindowPtr        theWindow )
  341. {
  342.     App_Key(
  343.         theWindow,
  344.         theEvent->message & (charCodeMask | keyCodeMask),
  345.         theEvent->modifiers );
  346. }
  347.  
  348. //---------------------------------------------------------------------------
  349.  
  350. static void Event_DoCmdKeyDownEvent(
  351.     EventRecord        *theEvent,
  352.     WindowPtr        theWindow )
  353. {
  354.     App_CmdKey( theWindow, theEvent->message & charCodeMask, theEvent->modifiers );
  355. }
  356.  
  357. //---------------------------------------------------------------------------
  358.  
  359. void Event_HandleKeyDownEvent(
  360.     EventRecord        *theEvent )
  361. {
  362. long         aMenu;
  363. WindowPtr    theWindow;
  364.  
  365.     theWindow = App_GetFrontDocWindow();
  366.     if (theEvent->modifiers & cmdKey)
  367.     {
  368.     
  369. #ifdef USE_CALIB
  370.  
  371.         if (gCALibExists)
  372.         {
  373.             OSErr        theErr;
  374.     
  375.             CAAdjustMenus();
  376.             if (theErr = CAError())
  377.                 ;        // handle the error
  378.         }
  379.  
  380. #endif
  381.  
  382.         App_AdjustMenus (theWindow);
  383.         aMenu = MenuKey( (char)(theEvent->message & charCodeMask) );
  384.         if (aMenu != 0L)
  385.             Menu_HandleMenu( aMenu, theWindow );
  386.         else
  387.             Event_DoCmdKeyDownEvent( theEvent, theWindow );
  388.     }
  389.     else
  390.         Event_DoKeyDownEvent( theEvent, theWindow );
  391. }
  392.  
  393. //---------------------------------------------------------------------------
  394.  
  395. void Event_HandleAutoKeyEvent(
  396.     EventRecord        *theEvent )
  397. {
  398. long        aMenu;
  399. WindowPtr    theWindow;
  400.  
  401.     theWindow = App_GetFrontDocWindow();
  402.     if (theEvent->modifiers & cmdKey)
  403.     {
  404.         aMenu = MenuKey( (char)(theEvent->message & charCodeMask) );
  405.         if (aMenu != 0L)
  406.             Menu_HandleMenu( aMenu, theWindow );
  407.         else
  408.             Event_DoCmdKeyDownEvent( theEvent, theWindow );
  409.     }
  410.     else
  411.         Event_DoKeyDownEvent( theEvent, theWindow );
  412. }
  413.  
  414. //---------------------------------------------------------------------------
  415.  
  416. void Event_HandleKeyUpEvent(
  417.     EventRecord        *theEvent )
  418. {
  419. }
  420.  
  421. //---------------------------------------------------------------------------
  422. // Event_HandleActivateEvent - SetPort the window, call doActivate or doDeactivate
  423. // depending upon the event modifier.
  424.  
  425. void Event_HandleActivateEvent(
  426.     EventRecord        *theEvent )
  427. {
  428. WindowPtr    theWindow;
  429.  
  430.     // if there is a window and it's ours, handle it.
  431.     theWindow = (WindowPtr)theEvent->message;
  432.     if (Win_IsAppWindow( theWindow ))
  433.     {
  434.         if (theEvent->modifiers & activeFlag)
  435.             App_Activate( theWindow );
  436.         else
  437.             App_Deactivate( theWindow );
  438.     } 
  439.     
  440.  
  441. }
  442.  
  443. //---------------------------------------------------------------------------
  444. // Event_HandleUpdateEvent - SetPort the window. BeginUpdate, call appUpdate, EndUpdate.
  445. // also handle updates for dialogs by calling DrawDialog.
  446.  
  447. void Event_HandleUpdateEvent(
  448.     EventRecord        *theEvent )
  449. {
  450. WindowPtr    theWindow;
  451. GrafPtr        savePort;
  452.  
  453.     // get the window from the event.
  454.     theWindow = (WindowPtr)(theEvent->message);
  455.  
  456.     GetPort( &savePort );
  457.     SetPort( theWindow );
  458.  
  459.     // if there is a window and it's ours, update it.
  460.     if (Win_IsAppWindow( theWindow ))
  461.     {
  462.         Win_Update (theWindow);
  463.     }
  464.     // if the window is a dialog, draw it.
  465.     else if (Win_IsDialogWindow( theWindow ))
  466.     {
  467.         //BeginUpdate( theWindow );
  468.         DrawDialog( theWindow );
  469.         //EndUpdate( theWindow );
  470.     }
  471.  
  472.     SetPort( savePort );
  473. }
  474.  
  475. //---------------------------------------------------------------------------
  476.  
  477. void Event_HandleHighLevelEvent(
  478.     EventRecord        *theEvent )
  479. {
  480.  
  481.     AE_DoAppleEvent( theEvent );
  482. }
  483.  
  484. //---------------------------------------------------------------------------
  485.  
  486. void Event_HandleOSEvent(
  487.     EventRecord        *theEvent )
  488. {
  489. Boolean        isSuspendEvent;
  490.  
  491.     switch ((theEvent->message >> 24) & 0x0FF)
  492.     {
  493.         case mouseMovedMessage:
  494.             Event_DoIdle();    // mouseMovedMessage is also an idle event
  495.             if (!gInBackground)
  496.                 App_AdjustCursor( App_GetFrontDocWindow(), theEvent->where, gMouseRgn );
  497.             break;
  498.  
  499.         case suspendResumeMessage:
  500.             isSuspendEvent = ((theEvent->message & resumeFlag) == 0L);
  501.  
  502.             gInBackground = isSuspendEvent;
  503.             
  504.             if (isSuspendEvent)
  505.             {
  506.                 App_Suspend();
  507.             }
  508.             else
  509.             {
  510.                 App_Resume();
  511.             }
  512.             App_ShowHideToolWindows( !gInBackground );
  513.             break;
  514.  
  515.         default:
  516.             break;
  517.     }
  518. }
  519.  
  520. //---------------------------------------------------------------------------
  521.  
  522. void Event_HandleDiskEvent(
  523.     EventRecord        *theEvent )
  524. {
  525. Point    thePt;
  526.  
  527.     if (HiWord( theEvent->message ) != 0)
  528.     {
  529.         thePt.h = thePt.v = 0x40;
  530.         DIBadMount( thePt, theEvent->message );
  531.     }
  532. }
  533.  
  534.